package org.jeecg.modules.FinanceModule.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.FinanceModule.entity.MonthStatementEntity;
import org.jeecg.modules.FinanceModule.service.MonthStatementsService;
import org.jeecg.modules.gwcjava.jiekou.entity.MlccUserInfo;
import org.jeecg.modules.zhoupackage.mapper.CartMlccUserInfoMapper;
import org.jeecg.modules.zonglzp.entity.store;
import org.jeecg.modules.zonglzp.service.orderAfterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/MonthStatement")
public class MonthStatementsController {

    @Autowired
    org.jeecg.modules.zonglzp.service.orderAfterService orderAfterService;

    @Autowired
    CartMlccUserInfoMapper userMapper;

    @Autowired
    MonthStatementsService service;

    @GetMapping("/MonthStatementList")
    public Result<?> MonthStatementList(
            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
            @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
            @RequestParam(name = "status", defaultValue = "10") int status,
            String realname
    ){
        Page<MonthStatementEntity> page=new  Page<MonthStatementEntity>(pageNo,pageSize);
        IPage<MonthStatementEntity> pageList=service.getMonthStatementList(page,status,realname);
        return Result.ok(pageList);
    }


    /**
     * 新增欠钱账单
     */
    @PostMapping("/addMonthStatement")
    public Result<?> addMonthStatement(
            @RequestBody MonthStatementEntity entity
    ){
        try {
//        String userId=service.getuserIdbycompanyCode(entity.getCompanyCode());
//        if(userId==null){
//            return Result.error("不存在此用户，请修改客户编码");
//        }
//        entity.setUserId(userId);
        int count=service.addMonthStatement(entity);
        if(count>0){
            return Result.ok("新增账单成功");
        }else {
            return Result.ok("新增账单失败");
        }
        }catch (Exception e){
            e.printStackTrace();
            //手动回滚事务
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return Result.ok("新增失败");
        }
    }


    /**
     * 编辑总账单
     */
    @PostMapping("/updateMonthStatement")
    public Result<?> updateMonthStatement(
            @RequestBody MonthStatementEntity entity
    ){
        try {
            int count=service.updateMonthStatement(entity);
            return Result.ok("编辑成功");
        }catch (Exception e){
            e.printStackTrace();
            return Result.ok("编辑失败");
        }
    }



    /**
     * 查询这个账单里的账单明细条列表
     * @param pageSize
     * @param pageNo
     * @param billid
     * @return
     */
    @GetMapping("/smallMonthStatementList")
    public Result<?> smallMonthStatementList(
            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
            @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
            @RequestParam(name = "billid") String billid,
            String orderSn
    ){
        Page<MonthStatementEntity> page=new  Page<MonthStatementEntity>(pageNo,pageSize);
        IPage<MonthStatementEntity> pageList=service.getsmallMonthStatementList(page,billid,orderSn);
        return Result.ok(pageList);
    }


    /**
     * 查询所有人员
     */
    @GetMapping("/getAlluser")
    public Result<?> getAlluser(){
        QueryWrapper<MlccUserInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("status",0);
        List<MlccUserInfo> infos = userMapper.selectList(wrapper);
        return Result.ok(infos);
    }


    /**
     * 新增月结账单
     */
      @GetMapping("/addMonthzhagndan")

      public Result<?> addMonthzhagndan(
              @RequestParam(name = "userId") String userId
      ){
          try {
              service.dingshiTongjizhangdan(userId);
              return Result.ok("操作成功");
          }catch (Exception e){
              e.printStackTrace();
              return Result.ok("操作失败");
          }

      }


}
